package es.biciapp.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import es.biciapp.data.QueryUsuario;
import es.biciapp.util.Utilidades;

public class PanelLogin extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PanelLogin panelLogin;
	private PanelMenu panelMenu;
	private PanelRegistro panelRegistro;
	private Frame frame;

	public PanelLogin() {
		initComponents();
		panelLogin = this;
		

	}

	@SuppressWarnings("unchecked")
	private void initComponents() {

		jTextFieldNick = new javax.swing.JTextField();
		jPasswordFieldPassword = new javax.swing.JPasswordField();
		jLabelNick = new javax.swing.JLabel();
		jLabelPassword = new javax.swing.JLabel();
		jButtonLogin = new javax.swing.JButton();
		jButtonNuevoUsuario = new javax.swing.JButton();
		jButtonRecordarPassword = new javax.swing.JButton();

		jPasswordFieldPassword.setText("jPasswordField1");

		jLabelNick.setFont(new java.awt.Font("Nina", 0, 14)); // NOI18N
		jLabelNick.setText("Nick");

		jLabelPassword.setFont(new java.awt.Font("Nina", 0, 14)); // NOI18N
		jLabelPassword.setText("Password");

		jButtonLogin.setText("Login");
		jButtonLogin.addActionListener(this);

		jButtonNuevoUsuario.setText("Nuevo Usuario");
		jButtonNuevoUsuario.addActionListener(this);

		jButtonRecordarPassword.setText("Recordar Password");
		jButtonRecordarPassword.addActionListener(this);

		jPasswordFieldPassword.setText("");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jLabelNick,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														84,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabelPassword))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(jTextFieldNick)
												.addComponent(
														jPasswordFieldPassword,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														188, Short.MAX_VALUE))
								.addGap(81, 81, 81))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup().addGap(46, 46, 46)
								.addComponent(jButtonNuevoUsuario)
								.addGap(18, 18, 18)
								.addComponent(jButtonRecordarPassword)
								.addGap(18, 18, 18).addComponent(jButtonLogin)
								.addContainerGap(53, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(78, 78, 78)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jTextFieldNick,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														29,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabelNick))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jPasswordFieldPassword,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														30,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabelPassword))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										79, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jButtonLogin)
												.addComponent(
														jButtonNuevoUsuario)
												.addComponent(
														jButtonRecordarPassword))
								.addGap(43, 43, 43)));
	}// </editor-fold>

	// Variables declaration - do not modify
	private javax.swing.JButton jButtonLogin;
	private javax.swing.JButton jButtonNuevoUsuario;
	private javax.swing.JButton jButtonRecordarPassword;
	private javax.swing.JLabel jLabelNick;
	private javax.swing.JLabel jLabelPassword;
	private javax.swing.JPasswordField jPasswordFieldPassword;
	private javax.swing.JTextField jTextFieldNick;

	// End of variables declaration

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == jButtonLogin) {
			String usuario = jTextFieldNick.getText();
			String password = jPasswordFieldPassword.getText();
			HashMap<String, String> mapa = QueryUsuario.getPassword(usuario);
			if (mapa.containsKey(usuario)) {
				if (mapa.get(usuario).equals(password)) {
					System.out.println("Usuario Existe");
					panelLogin.setVisible(false);
					frame = Utilidades.getFrameContenedor(this);
					frame.remove(panelLogin);
					panelMenu = new PanelMenu();
					panelMenu.setVisible(true);
					frame.add(panelMenu);
					frame.setSize(500, 500);
					frame.repaint();

				}
			} else if(jTextFieldNick.getText().isEmpty()||jPasswordFieldPassword.getText()) {
				JOptionPane.showMessageDialog(null, "El usuario o la contrase�a introducida no existen.");
			}
		}

		if (e.getSource() == jButtonNuevoUsuario) {
			System.out.println("Registro");
			panelLogin.setVisible(false);
			frame = Utilidades.getFrameContenedor(this);
			frame.remove(panelLogin);
			panelRegistro = new PanelRegistro();
			panelRegistro.setVisible(true);
			frame.add(panelRegistro);
			frame.setSize(366, 402);
			frame.setResizable(true);
			frame.repaint();
		}

	}
}
